// pages/my/order/order.js
import Config from "../../common/config.js";
var sessionId = wx.getStorageSync('sessionId')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: {},
    id: "",
    noData: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  //取消订单
  cancleorder: function(e) {
    var that = this
    console.log(e.currentTarget.dataset.id);
    // if(){

    // }else{

    // }
    wx.request({
      url: Config.url + '/api/transactionOrder/cancel_order',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': wx.getStorageSync('sessionId')
      },
      data: {
        id: e.currentTarget.dataset.id
      }, //订单id
      success: function(res) {
        wx.request({
          url: Config.url + '/api/transactionOrder/find_all_status_order',
          method: 'POST',
          header: {
            'content-type': 'application/json',
            'accessToken': sessionId
          },
          success(res) {
            console.log(res.data.code);

            console.log(res);
            if (res.data.code == 203) {
              that.setData({
                noData: false
              })
            } else {
              that.setData({
                orderList: res.data.data.allOrder,
              })
            }
          }
        })
      }
    });
    wx.request({
      url: Config.url + '/api/transactionOrder/find_all_status_order',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': sessionId
      },
      success(res) {
        console.log(res.data.code);

        console.log(res);
        if (res.data.code == 203) {
          that.setData({
            noData: false
          })
        } else {
          that.setData({
            orderList: res.data.data.allOrder,
          })
        }
      }
    })
  },
  goDetail: function(e) {
    console.log(e.currentTarget.dataset.status);
    // if (e.currentTarget.dataset.status == 1) {
    //   wx.navigateTo({
    //     url: '../orderDetails/orderDetails?id=' + e.currentTarget.dataset.id + '&bookId=' + e.currentTarget.dataset.bookid + '&pagesize=' + e.currentTarget.dataset.pagesize + '&amount=' + e.currentTarget.dataset.amount + '&status=' + e.currentTarget.dataset.status + '&buy=' + e._relatedInfo.anchorTargetText
    //   })
    // } else if (e.currentTarget.dataset.status != 1) {
      wx.navigateTo({
        url: '../dingdanDetails/dingdanDetails?id=' + e.currentTarget.dataset.id,
      })
    // }
  },
  gopay:function(e){
     wx.navigateTo({
        url: '../orderDetails/orderDetails?id=' + e.currentTarget.dataset.id + '&bookId=' + e.currentTarget.dataset.bookid + '&pagesize=' + e.currentTarget.dataset.pagesize + '&amount=' + e.currentTarget.dataset.amount + '&status=' + e.currentTarget.dataset.status + '&buy=' + e._relatedInfo.anchorTargetText
      })
  },
  //查看物流
  tologistics: function(e) {
    console.log(e.currentTarget.dataset.wuliu);
    wx.navigateTo({
      url: '../logistics/logistics?wuliuid=' + e.currentTarget.dataset.wuliu,
    })
  },
  goagain: function(e) {
    wx.navigateTo({
      url: '../againorder/againorder?id=' + e.currentTarget.dataset.id + '&bookId=' + e.currentTarget.dataset.bookid + '&pagesize=' + e.currentTarget.dataset.pagesize + '&amount=' + e.currentTarget.dataset.amount + '&status=' + e.currentTarget.dataset.status
    })
  },
  // gobuy:function(){
  //   wx.navigateTo({
  //     url: '../orderDetails/orderDetails?buy='+2,
  //   })
  // }
  onShow: function() {
    var sessionId = wx.getStorageSync('sessionId')
    var that = this
    wx.request({
      url: Config.url + '/api/transactionOrder/find_all_status_order',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': sessionId
      },
      success(res) {
        //       // --------
        if (res.data.code == 600) {
          wx.login({
            success: function(res) {
              console.log(res.code)
              //发送请求
              wx.request({
                url: 'https://zhangyushu.liangziqishi.shop/api/app/login', //接口地址
                data: {
                  code: res.code
                },
                method: 'POST',
                header: {
                  'content-type': 'application/json', //默认值
                  'accessToken': sessionId
                },
                success: function(res) {
                  console.log(res.data)
                  wx.setStorageSync("session_key", res.data.data.data.session_key);
                  // wx.setStorageSync("sessionId",res.data.data.data.sessionId);
                  wx.setStorageSync("openid", res.data.data.data.openid)
                  wx.setStorage({
                    key: 'sessionId',
                    data: res.data.data.data.sessionId,
                  })
                }
              });
            }
          })


          wx.request({
            url: Config.url + '/api/transactionOrder/find_all_status_order',
            method: 'POST',
            header: {
              'content-type': 'application/json',
              'accessToken': sessionId
            },
            success(res) {

              console.log(res.data.code);
              if (res.data.code == 203) {
                that.setData({
                  noData: false
                })
              } else {
                that.setData({
                  orderList: res.data.data.allOrder,
                })
              }
              console.log(that.data.allOrder)
            }
          })
        }
        if (res.data.code == 203) {
          that.setData({
            noData: false
          })
        } else {
          that.setData({
            orderList: res.data.data.allOrder,
          })
        }
      }
    })
  }

})